"""
Case Type   : 基础功能-兼容B库语法-optimize
Case Name   : 表的dml操作过程中进行optimize并发测试
Create At   : 2024/10/25
Owner       : peilinqian
Description :
    step1: 创建测试表并插入数据
    step2: 以线程开启事务，对表进行delete操作，20s后再提交
    step3: 步骤2执行过程中，optimize表
Expect      :
    step1: 创建测试表并插入数据 expect: 创建插入成功
    step2: 以线程开启事务，对表进行delete操作，20s后再提交 expect: 操作成功
    step3: 步骤2执行过程中，optimize表 expect:optimize操作阻塞至步骤2执行完成
History     :
"""
import os
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_sh = CommonSH('PrimaryDbBmode')
        self.constant = Constant()
        self.t_name = 't_compatibility_optimize_0009'
        self.err_flag = 'ERROR:  Lock wait timeout'

    def test_main(self):
        step_txt = '----step1: 创建测试表并插入数据，expect: 创建插入成功----'
        self.log.info(step_txt)
        create_sql = f"drop table if exists {self.t_name};" \
                     f"create table {self.t_name}(c1 int,c2 text);" \
                     f"insert into  {self.t_name} values" \
                     f"(generate_series(1,1000000),'a');"
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn('INSERT', create_result, '执行失败:' + step_txt)

        step2_txt = '----step2: 以线程开启事务，对表进行delete操作，20s后再提交，expect: 操作成功---'
        self.log.info(step2_txt)
        dml_sql = f'begin;' \
                  f'delete from {self.t_name} where c1 > 5;' \
                  f'select pg_sleep(20);' \
                  f'end;'
        session1 = ComThread(self.pri_sh.execut_db_sql, args=(dml_sql,))
        session1.setDaemon(True)
        session1.start()
        time.sleep(2)

        step3_txt = '----step3: 步骤2执行过程中，optimize表 ' \
                    'expect:optimize操作阻塞至步骤2执行完成---'
        self.log.info(step3_txt)
        begin_time = time.time()
        sql = f'select sysdate();' \
              f'optimize verbose no_write_to_binlog table {self.t_name};' \
              f'select sysdate();'
        optimize_result = self.pri_sh.execut_db_sql(sql)
        self.log.info(optimize_result)
        end_time = time.time()
        delta_time = end_time - begin_time
        self.log.info(delta_time)
        self.assertGreater(delta_time, 16, '执行失败:' + step_txt)
        self.assertIn(self.constant.VACUUM_SUCCESS_MSG, optimize_result,
                      '执行失败:' + step3_txt)

        self.log.info("----步骤2事务执行结果----")
        session1.join()
        session1_result = session1.get_result()
        self.log.info(session1_result)
        self.assertIn(self.constant.COMMIT_SUCCESS_MSG, session1_result,
                      '执行失败:' + step2_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----step:清理数据; expect:清理成功----'
        self.log.info(step_txt)
        drop_sql = f"drop table if exists {self.t_name};"
        result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(result)

        self.log.info('----teardown断言----')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result,
                      "执行失败" + step_txt)

        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
